package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.dto.goods.GoodsUnitListDTO;
import cn.ulc.socket.admin.model.param.goods.GoodsUnitSaveParam;
import cn.ulc.socket.admin.service.goods.GoodsUnitServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/goodsUnits")
public class GoodsUnitCtrl {
    private static final Log log = LogFactory.get();
    @Autowired
    GoodsUnitServ unitServ;


    @GetMapping("")
    public Result findList(String name) {
        GoodsUnitListDTO dto = unitServ.findList(name);
        return Result.success(dto);
    }
    @GetMapping("/{id}")
    public Result findDetail(@PathVariable Long id) {
        return Result.success(unitServ.findDetail(id));
    }
    @PostMapping
    public Result save(@Validated @RequestBody GoodsUnitSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商品详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(unitServ.save(param));
    }
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody GoodsUnitSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商品详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(unitServ.update(id,param));
    }
    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(unitServ.deleted(id));
    }
    //改变状态
    @PutMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Long id , AvailableStatus status){
        return Result.success(unitServ.changeStatus(id,status));
    }
    //排序
    @PutMapping("/changeSort")
    public Result changeSort(String type, Long id1, Long id2){
        return Result.success(unitServ.changeSort(type,id1,id2));
    }

}
